<?php
/**
 *Create by PhpStorm
 *User:Kendy
 *Date:2019/10/10/010
 *Time:11:49
 */
namespace app\common\model;
use think\Model;
use think\Db;
use app\common\model\LogOrderModel as lo;
class tourOrderModel extends Model
{
    // 确定链接表名
    protected $name = 'tour_order';
    /**
     * 根据搜索条件获取所有的景点订单数据
     * @param $where  查询条件
     * @param $offset
     * @param $limit
     * @return mixed
     */
    public function getTourOrderList($where ,$offset, $limit)
    {
        $resultData = $this
            ->alias('to')
            ->join('tours t','to.tour_id=t.tour_id','left')
            ->where($where)
            ->field('to.order_id,to.commission_fee,to.procedure_fee,t.tour_name,to.order_sn,to.tour_id,to.user_id,to.linkman,to.tel,to.ticket_count,
            to.ticket_date,to.order_status,to.ticket_price,to.order_price,to.pay_type,to.is_pay,
            to.is_appraise,to.is_refund,to.order_unique,to.is_del,to.create_time')
            ->order('order_id desc')
            ->limit($offset, $limit)
            ->select(); 
        if(count($resultData)>0)
        {
            foreach ($resultData as $key => $v)
            {
                $resultData[$key]['order_status'] = lvOrderStatus($v['order_status']);
				$resultData[$key]['ticket_date'] =  date('Y-m-d H:i:s',$v['ticket_date']);
                $resultData[$key]['create_time'] =  date('Y-m-d H:i:s',$v['create_time']);
				
            }
        }
        return $resultData;
    }


    /**
     * 根据搜索条件获取所有的景点订单数量
     * @param $where  查询条件
     * @return mixed
     */
    public function  getTourOrderCount($where)
    {
        return $this
            ->alias('to')
            ->join('tours t','to.tour_id=t.tour_id','left')
            ->where($where)
            ->count();
    }

    /**根据搜索条件获取单条的景点订单信息
     * @param $where 查询条件
     * @return mixed
     */
    public function  getTourOrderById($where)
    {
        $resultData = $this
            ->alias('to')
            ->join('tours t','to.tour_id=t.tour_id','left')
            ->where($where)
            ->field('to.order_id,t.tour_name,to.order_sn,to.tour_id,to.user_id,to.linkman,to.tel,to.ticket_count,
            to.ticket_date,to.order_status,to.ticket_price,to.order_price,to.pay_type,to.is_pay,
            to.is_appraise,to.is_refund,to.order_unique,to.is_del,to.create_time')
            ->find();
        if(empty($resultData)) return [];
        $resultData['pay_type'] = lvPayType($resultData['pay_type']);
        $resultData['order_status'] = lvOrderStatus($resultData['order_status']);
		$resultData['ticket_date'] = date('Y-m-d H:i:s',$resultData['ticket_date']);
        $resultData['create_time'] = date('Y-m-d H:i:s',$resultData['create_time']);
        return $resultData;
    }
  /**
   * [submitTourOrder 提交景点订单]
   * @Author   kendy
   * @DateTime 2019-11-04T09:13:32+0800
   * @return   [type]                   [description]
   */
   public function submitTourOrder()
   {
     $linkman = input('post.linkman');
     $tel = input('post.tel');
     $ticket_count = input('post.ticket_count');
     $ticket_date = input('post.ticket_date');
     $tid = input('post.tid');
     $userId = input('post.uid');
     if(empty($linkman))
        return apiReturn('400','取票人信息为空',0);
     if(empty($tel))
        return apiReturn('400','取票人手机号为空',0);
     if(empty($ticket_count))
        return apiReturn('400','门票数为空',0);
     if(empty($ticket_date))
        return apiReturn('400','门票使用日期为空',0);
     //查看是否有该门票
     $ticket = Db::name('tour_ticket')
     ->alias('tt')
     ->field('tt.tour_id,price,tt.tname,tt.is_discount,tour_name,discount_price')
     ->join('tours t','tt.tour_id=t.tour_id','left')
     ->where('tt.tid',$tid)
     ->find();
     if(empty($ticket)) return apiReturn('400','购买的门票信息为空',0);

     $order['tour_id'] = $ticket['tour_id'];
     //计算订单价格
     if($ticket['is_discount']) $price = $ticket['discount_price'];
      else  $price = $ticket['price'];
      //生成订单
      Db::startTrans();
      try{ 
        $orderSn = makeOrderSn(); 
           //计算该景点的订单佣金
        $commissionRate = Db::name('tours') 
        ->where(['is_del'=>0,'is_pass'=>1,'tour_id'=>$ticket['tour_id']])
        ->value("commission_rate");
        if(empty($commissionRate))
        $commissionRate = 0;  
      //创建订单   
        $order['type_id'] = $tid; 
        $order['order_sn'] = $orderSn; 
        $order['user_id'] = $userId;
        $order['linkman'] = $linkman;
        $order['tel'] = $tel;
        $order['ticket_count'] = $ticket_count;
        $order['ticket_date'] = strtotime($ticket_date);
        $order['ticket_price'] =  $price;
        $order['order_price'] =  $price *  $ticket_count;; 
        $order['create_time'] =  time();
        $order['commission_rate'] =  $commissionRate;
        $order['cancel_code'] = 'tour_cancle_code_'.getRandNumber();
        $result = $this->isUpdate(false)->save($order); 
        if(false !== $result)
        {
          //需要给用户返回的信息
            $orderInfo['order_sn'] = $order['order_sn'];
            $orderInfo['tour_name'] = $ticket['tour_name'];
            $orderInfo['tname'] = $ticket['tname'];
            $orderInfo['ticket_date'] = date('Y-m-d',$this->ticket_date);
            $orderInfo['ticket_count'] = $this->ticket_count;
            $orderInfo['price'] = $price * $this->ticket_count;;  
           //创建订单记录
            $orderId = $this->order_id; 
            $logOrder = new lo();
            $logOrder->addLogOrder($orderId,2, $userId, 0, '下单成功，等待用户支付');
            //修改总销售量
            Db::name('tours')->where('tour_id',$order['tour_id'])->setInc('sale_sum',1);
            //给用户发送消息提示1景点订单 2酒店订单 3旅行社订单
            send_user_message(1, 2, $userId, $orderSn,$ticket['tour_name'],$orderId,1);
            //给商户发送消息 
            
        } 
        Db::commit();
        return apiReturn('200','提交订单成功',$orderInfo);  
      }catch(\Exception $e){
        Db::rollback();
        return apiReturn('400', $e->getMessage(),0); 
      }

   }

}